<?php


namespace app\mingteng\model;

use think\Db;
use think\Model;

class Rechargeable extends Model
{

    /**
     * 虚拟卡
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function virtually($params,$user){

        $prefix='';
        switch ($params['denomination'])
        {
            case "1000":
                $prefix = 'XT';
                break;
            case "2000":
                $prefix = 'XF';
                break;
            case "5000":
                $prefix = 'XG';
                break;
            case "10000":
                $prefix = 'XH';
                break;
        }
        $all = [];
        for($i=1;$i<=$params['number'];$i++){
            $count = Db::name('rechargeable')->where('types',1)->where('denomination',$params['denomination'])->count();
            $card_number = $prefix.str_pad($count+$i,6,0,STR_PAD_LEFT);

            $all[$i]['uid'] =  $user['userId'];
            $all[$i]['userMobile'] =  $params['userMobile'];
            $all[$i]['types'] =  $params['types'];
            $all[$i]['card_number'] =  $card_number;
            $all[$i]['card_password'] =  $this->random_str(8);
            $all[$i]['denomination'] =  $params['denomination'];
            $all[$i]['substantialCharge'] =  $params['substantialCharge'];
            $all[$i]['createtime'] =  time();
        }
        $rechargeableId = Db::name('rechargeable')->insertAll($all);
        return $rechargeableId;
    }


    /**
     * 实体卡
     * @return \think\response\Json
     */
    public function entity($params){

        $prefix='';
        switch ($params['denomination'])
        {
            case "1000":
                $prefix = 'T';
                break;
            case "2000":
                $prefix = 'F';
                break;
            case "5000":
                $prefix = 'G';
                break;
            case "10000":
                $prefix = 'H';
                break;
        }
        $all = [];
        for($i=1;$i<=$params['number'];$i++){
            $count = Db::name('rechargeable')->where('types',2)->where('denomination',$params['denomination'])->count();
            $card_number = $prefix.str_pad($count+$i,6,0,STR_PAD_LEFT);

            $all[$i]['types'] =  $params['types'];
            $all[$i]['card_number'] =  $card_number;
            $all[$i]['card_password'] =  $this->random_str(8);
            $all[$i]['denomination'] =  $params['denomination'];
            $all[$i]['createtime'] =  time();
        }
        $rechargeableId = Db::name('rechargeable')->insertAll($all);
        return $rechargeableId;
    }

    /*
 *  生成随机字符串
 *
 *   $length    字符串长度
 */
    function  random_str($length) {
        // 密码字符集，可任意添加你需要的字符
        $chars = 'abcdefghijklm01234nopqrstuvwxyzABCDEFGHIJKL56789MNOPQRSTUVWXYZ';
        $str = '';
        for($i = 0; $i < $length; $i++)
        {
            // 这里提供两种字符获取方式
            // 第一种是使用 substr 截取$chars中的任意一位字符；
            // 第二种是取字符数组 $chars 的任意元素
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
//            $str .= $chars[mt_rand(0, strlen($chars) - 1)];
        }
        return $str;
    }
}